package com.yoyo.yyadsdk.broad;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.anye.greendao.gen.ShortcutEntityDao;
import com.yoyo.yyadsdk.bean.dao.LocalShortcutEntity;
import com.yoyo.yyadsdk.bean.dao.ShortcutEntity;
import com.yoyo.yyadsdk.config.PullFeature;
import com.yoyo.yyadsdk.demo.DBManager;
import com.yoyo.yyadsdk.util.AlarmUtils;
import com.yoyo.yyadsdk.util.GreenDaoManager;
import com.yoyo.yyadsdk.util.TimeUtils;

public class ShortCutClickReceiver extends BroadcastReceiver {
    public static final String ACTION = "com.yoyo.yyadsdk.broad.ShortCutClickReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null && ACTION.equals(intent.getAction())) {
            int shortcutId = intent.getIntExtra("shortcut", -1);
            if (shortcutId > 0) {
                LocalShortcutEntity l = DBManager.getLocalShortcutEntity(shortcutId);
                if (l != null) {
                    l.setClickCount(l.getClickCount() + 1);
                    ShortcutEntity shortcutEntity = GreenDaoManager.getInstance().getDaoSession().getShortcutEntityDao().queryBuilder()
                            .where(ShortcutEntityDao.Properties.Id.eq(shortcutId)).unique();
                    if (shortcutEntity != null) {
                        if (l.getClickCount() >= shortcutEntity.getFrequency()) {
                            AlarmUtils.addTask(PullFeature.ALARM_DEL_SHORT_CUT, shortcutEntity.getTitle().concat(",").concat(String.valueOf(shortcutEntity.getId())),
                                    0, false, 0);
                            l.setIsDel(true);
                        }
                    }
                    DBManager.saveLocalShortcutEntity(l);
                }
            }
        }
    }
}